package com.ruoyi.bus.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.bus.domain.WitRouteSite;
import com.ruoyi.bus.mapper.WitRouteMapper;
import com.ruoyi.bus.domain.WitRoute;
import com.ruoyi.bus.service.IWitRouteService;

/**
 * 路线Service业务层处理
 * 
 * @author caiyanshan
 * @date 2021-04-14
 */
@Service
public class WitRouteServiceImpl implements IWitRouteService 
{
    @Autowired
    private WitRouteMapper witRouteMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询路线
     * 
     * @param routeId 路线ID
     *                需要做缓存(查缓存，编辑后提交需清缓存)
     * @return 路线
     */
    @Override
    public WitRoute selectWitRouteById(Long routeId)
    {
        String key = "route_key_" + routeId;
        WitRoute witRoute = redisCache.getCacheObject(key);
        if(witRoute != null){
            return witRoute;
        }
        witRoute = witRouteMapper.selectWitRouteById(routeId);
        if(witRoute == null){
            throw new RuntimeException("该路线已被删除,路线ID为:" + routeId);
        }
        redisCache.setCacheObject(key, witRoute, 5, TimeUnit.MINUTES);//缓存5分钟
        return witRoute;
    }
    
    
    public List<WitRoute> selectRouteByTwoSideId(Map<String, Object> paramMap){
        return witRouteMapper.selectRouteByTwoSideId(paramMap);
    }
    
    public List<WitRoute> listRouteByNowPlan(WitRoute route){
        return witRouteMapper.listRouteByNowPlan(route);
    }
    
    public List<WitRoute> myRoute(Long userId){
        return witRouteMapper.myRoute(userId);
    }
    
    @Override
    public WitRoute selectWitRouteByIdNoCache(Long routeId) {
        return witRouteMapper.selectWitRouteById(routeId);
    }

    /**
     * 查询路线列表
     * 
     * @param witRoute 路线
     * @return 路线
     */
    @Override
    public List<WitRoute> selectWitRouteList(WitRoute witRoute)
    {
        return witRouteMapper.selectWitRouteList(witRoute);
    }

    /**
     * 新增路线
     * 
     * @param witRoute 路线
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWitRoute(WitRoute witRoute)
    {
        witRoute.setCreateTime(DateUtils.getNowDate());
        int rows = witRouteMapper.insertWitRoute(witRoute);
        insertWitRouteSite(witRoute);
        return rows;
    }

    /**
     * 修改路线
     * 
     * @param witRoute 路线
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWitRoute(WitRoute witRoute)
    {
        String key = "route_key_" + witRoute.getRouteId();
        redisCache.deleteObject(key);//每次更新操作都需要删除相关路线缓存
/*        witRouteMapper.deleteWitRouteSiteByRouteId(witRoute.getRouteId());
        insertWitRouteSite(witRoute);*/
        return witRouteMapper.updateWitRoute(witRoute);
    }

    /**
     * 批量删除路线,需要同步删除预约数据
     * 
     * @param routeIds 需要删除的路线ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWitRouteByIds(Long[] routeIds)
    {
        witRouteMapper.deleteWitRouteSiteByRouteIds(routeIds);
        return witRouteMapper.deleteWitRouteByIds(routeIds);
    }

    /**
     * 删除路线信息
     * 
     * @param routeId 路线ID
     * @return 结果
     */
    @Override
    public int deleteWitRouteById(Long routeId)
    {
        witRouteMapper.deleteWitRouteSiteByRouteId(routeId);
        return witRouteMapper.deleteWitRouteById(routeId);
    }

    /**
     * 新增站点路线信息
     * 
     * @param witRoute 路线对象
     */
    public void insertWitRouteSite(WitRoute witRoute)
    {
        List<WitRouteSite> witRouteSiteList = witRoute.getWitRouteSiteList();
        Long routeId = witRoute.getRouteId();
        if (StringUtils.isNotNull(witRouteSiteList))
        {
            List<WitRouteSite> list = new ArrayList<WitRouteSite>();
            for (WitRouteSite witRouteSite : witRouteSiteList)
            {
                witRouteSite.setRouteId(routeId);
                list.add(witRouteSite);
            }
            if (list.size() > 0)
            {
                witRouteMapper.batchWitRouteSite(list);
            }
        }
    }
    
    public List<WitRouteSite> myCommonRoute(Long userId){
        return witRouteMapper.myCommonRoute(userId);
    }
    
    public Integer selectCountRoute(){
        return witRouteMapper.selectCountRoute();
    }
}
